package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallAddress;
import org.linlinjava.litemall.db.mapper.LitemallAddressDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallAddressService {
    @Resource
    private LitemallAddressDao addressDao;

    public List<LitemallAddress> queryByUid(Integer uid) {
    	
    	LitemallAddress address = new LitemallAddress();
    	address.setUserId(uid);
    	address.setDeleted(false);
    	
    	return addressDao.template(address);
    	
		/*
		 * LitemallAddressExample example = new LitemallAddressExample();
		 * example.or().andUserIdEqualTo(uid).andDeletedEqualTo(false); return
		 * addressMapper.selectByExample(example);
		 */
    }

    public LitemallAddress query(Integer userId, Integer id) {
    	
    	LitemallAddress address = new LitemallAddress();
    	address.setUserId(userId);
    	address.setId(id);
    	address.setDeleted(false);
    	return addressDao.templateOne(address);
		/*
		 * LitemallAddressExample example = new LitemallAddressExample();
		 * example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(
		 * false); return addressMapper.selectOneByExample(example);
		 */
    }

    public int add(LitemallAddress address) {
        address.setAddTime(LocalDateTime.now());
        address.setUpdateTime(LocalDateTime.now());
        addressDao.insertTemplate(address, true);
        return 1;
		/* return addressMapper.insertSelective(address); */
    }

    public int update(LitemallAddress address) {
        address.setUpdateTime(LocalDateTime.now());
        return addressDao.updateTemplateById(address);
		/* return addressMapper.updateByPrimaryKeySelective(address); */
    }

    public void delete(Integer id) {
    	LitemallAddress address = new LitemallAddress();
    	address.setDeleted(true);
    	address.setId(id);
    	addressDao.updateTemplateById(address);
		/* addressMapper.logicalDeleteByPrimaryKey(id); */
    }

    public LitemallAddress findDefault(Integer userId) {
    	
    	LitemallAddress address = new LitemallAddress();
    	address.setUserId(userId);
    	address.setDeleted(false);
    	address.setIsDefault(true);
    	return addressDao.templateOne(address);
		/*
		 * LitemallAddressExample example = new LitemallAddressExample();
		 * example.or().andUserIdEqualTo(userId).andIsDefaultEqualTo(true).
		 * andDeletedEqualTo(false); return addressMapper.selectOneByExample(example);
		 */
    }

    public void resetDefault(Integer userId) {
        LitemallAddress address = new LitemallAddress();
        address.setIsDefault(false);
        address.setUpdateTime(LocalDateTime.now());
        addressDao.updateTemplateById(address);
		/*
		 * LitemallAddressExample example = new LitemallAddressExample();
		 * example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
		 * addressMapper.updateByExampleSelective(address, example);
		 */
    }

    public List<LitemallAddress> querySelective(Integer userId, String name, Integer page, Integer limit, String sort, String order) {
        LambdaQuery<LitemallAddress> query =  addressDao.createLambdaQuery();
        if (userId != null) {
        	query.andEq(LitemallAddress::getUserId, userId);
        }
        if (!StringUtils.isEmpty(name)) {
        	query.andLike(LitemallAddress::getName, "%" + name + "%");
        }
        query.andEq(LitemallAddress::getDeleted, false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
            //example.setOrderByClause(sort + " " + order);
        }
        
        query.limit(page, limit);
		List<LitemallAddress> list = query.select();
		return list;
        
		/*
		 * LitemallAddressExample example = new LitemallAddressExample();
		 * LitemallAddressExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (userId != null) { criteria.andUserIdEqualTo(userId); } if
		 * (!StringUtils.isEmpty(name)) { criteria.andNameLike("%" + name + "%"); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, limit); return
		 * addressMapper.selectByExample(example);
		 */
    }
}
